//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//

package io.gitee.caoxiaoyu97.messageconvert.util;


import io.gitee.caoxiaoyu97.messageconvert.exception.CxyException;
import com.alibaba.fastjson2.JSON;
import com.google.common.collect.Maps;
import com.google.common.xml.XmlEscapers;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.dom4j.Element;
import org.apache.commons.lang3.StringUtils;
import org.dom4j.Namespace;
import org.dom4j.QName;
import org.json.JSONObject;
import org.json.XML;


public abstract class MessageUtils {
    private static final Pattern formatPattern = Pattern.compile("(?m)^\\s*|\\t|\\r|\\n");

    public MessageUtils() {
    }

    public static Map<String, Object> messageToMap(String message) {
        Map<String, Object> messageMap = jsonToMap(message);
        if (messageMap == null) {
            messageMap = xmlToMap(message);
        }

        if (messageMap == null) {
            throw new CxyException("9000", "解析xml失败");
        } else {
            return messageMap;
        }
    }

    public static String removeBlank(String message) {
        Matcher matcher = formatPattern.matcher(message);
        return matcher.replaceAll("");
    }

    private static Map<String, Object> jsonToMap(String message) {
        Map<String, Object> messageMap = null;

        try {
            messageMap = JSON.parseObject(message);
        } catch (Exception var3) {
        }

        return messageMap;
    }

    private static Map<String, Object> xmlToMap(String message) {
        Map<String, Object> messageMap = null;
        if (!StringUtils.isEmpty(message)) {
            try {
                JSONObject jsonObject = XML.toJSONObject(message, true);
                Map<String, Object> mapTemp = jsonObject.toMap();
                List<String> mapKeys = new ArrayList(mapTemp.keySet());
                Object o = mapTemp.get(mapKeys.get(0));
                if (o instanceof Map) {
                    Map<String, Object> messageMapTemp = (Map) o;
                    messageMap = transferXmlOrJson(messageMapTemp, true);
                }
            } catch (Exception var7) {
            }
        }

        return messageMap;
    }

    public static Map<String, Object> transferXmlOrJson(Map<String, Object> valueMap, boolean isXml) {
        Map<String, Object> results = new HashMap();
        Set<Map.Entry<String, Object>> entries = valueMap.entrySet();

        Iterator<Map.Entry<String, Object>> iterator = entries.iterator();
        while (iterator.hasNext()) {
            Map.Entry<String, Object> entry = iterator.next();
            Object value;
            if (entry.getValue() == null) {
                value = null;
            } else if (entry.getValue() instanceof Map) {
                value = transferXmlOrJson((Map) entry.getValue(), isXml);
            } else if (entry.getValue() instanceof List) {
                List<Object> maps = (List) entry.getValue();
                value = transferListXmlOrJson(maps, isXml);
            } else if (entry.getValue() instanceof String) {
                String textValue = (String) entry.getValue();
                if (isXml) {
                    value = textValue.replace("\\", "\\\\").replace("\"", "\\\"");
                } else {
                    value = XmlEscapers.xmlContentEscaper().escape(textValue);
                }
            } else {
                value = entry.getValue();
            }
            results.put(entry.getKey(), value);
        }
        return results;
    }

    public static List<Object> transferListXmlOrJson(List<Object> objects, boolean isXml) {
        List<Object> results = new ArrayList(objects.size());
        Iterator var3 = objects.iterator();

        while (var3.hasNext()) {
            Object element = var3.next();
            if (element instanceof Map) {
                Map<String, Object> xmlMap = transferXmlOrJson((Map) element, isXml);
                results.add(xmlMap);
            } else if (element instanceof List) {
                results.add(transferListXmlOrJson((List) element, isXml));
            } else if (element instanceof String) {
                String textValue = (String) element;
                if (isXml) {
                    textValue = textValue.replace("\\", "\\\\").replace("\"", "\\\"");
                } else {
                    textValue = XmlEscapers.xmlContentEscaper().escape(textValue);
                }

                results.add(textValue);
            } else {
                results.add(element);
            }
        }

        return results;
    }

    private static Object xmlToMap(Element element) {
        Map<String, Object> map = Maps.newHashMap();
        List<Element> elements = element.elements();
        if (elements.size() > 0) {
            Map<String, Element> tempMap = new HashMap();
            Iterator var4 = elements.iterator();

            while (var4.hasNext()) {
                Element e = (Element) var4.next();
                tempMap.put(e.getName(), e);
            }

            Set<String> keySet = tempMap.keySet();
            Iterator var14 = keySet.iterator();

            while (true) {
                while (var14.hasNext()) {
                    String key = (String) var14.next();
                    Namespace namespace = ((Element) tempMap.get(key)).getNamespace();
                    List<Element> elementList = element.elements(new QName(key, namespace));
                    if (elementList.size() > 1) {
                        List<Object> list = new ArrayList();
                        Iterator var10 = elementList.iterator();

                        while (var10.hasNext()) {
                            Element ele = (Element) var10.next();
                            list.add(xmlToMap(ele));
                        }

                        map.put(key, list);
                    } else {
                        map.put(key, xmlToMap((Element) elementList.get(0)));
                    }
                }

                return map;
            }
        } else {
            String textValue = element.getText();
            if (!StringUtils.isEmpty(textValue)) {
                textValue = textValue.replace("\\", "\\\\");
                textValue = textValue.replace("\"", "\\\"");
            }

            map.put(element.getName(), textValue);
            if (!element.isRootElement()) {
                return textValue;
            } else {
                return map;
            }
        }
    }
}
